package com.yfree.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;

import com.yfree.YConfig;
import com.yfree.activities.YActivity;

import java.util.ArrayList;
import java.util.List;

public abstract class YBaseAdapter<T> extends BaseAdapter {
    protected YActivity context;
    protected List<T> list;
    private ArrayList<Integer> playedPosition;

    public YBaseAdapter(YActivity context, List<T> list) {
        this.context = context;
        this.list = list;
        playedPosition = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    public void addList(List<T> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    protected final void startAnim(View convertView, int position, boolean isGrid) {
        if (!playedPosition.contains(position)) {
            if (!isGrid) {
                // ListView的动画
                AnimationSet animationSet = new AnimationSet(true);
                animationSet.setDuration(YConfig.adapterAnimTime);
                animationSet.setFillAfter(true);
                TranslateAnimation tAnimation = new TranslateAnimation(convertView.getWidth(), 0, 0, 0);
                AlphaAnimation aAnimation = new AlphaAnimation(0, 1.0f);
                animationSet.addAnimation(tAnimation);
                animationSet.addAnimation(aAnimation);
                convertView.startAnimation(animationSet);
                playedPosition.add(position);
            } else {
                // GridView的动画
                AnimationSet animationSet = new AnimationSet(true);
                animationSet.setDuration(YConfig.adapterAnimTime);
                animationSet.setFillAfter(true);
                ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                AlphaAnimation aAnimation = new AlphaAnimation(0, 1.0f);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(aAnimation);
                convertView.startAnimation(animationSet);
                playedPosition.add(position);
            }
        }
    }

    protected final View inflateById(int layoutId) {
        return LayoutInflater.from(context).inflate(layoutId, null);
    }

    @Override
    public abstract View getView(int position, View convertView, ViewGroup viewGroup);

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
